package com.zr.service.impl;

import com.zr.dto.approval.*;
import com.zr.entity.Staff;
import com.zr.mapper.StaffMapper;
import com.zr.pojo.ReqDto;
import com.zr.pojo.Result;
import com.zr.service.ApprovalStaffService;
import com.zr.utils.DateUtil;
import com.zr.utils.JsonUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 员工信息查询、状态审批
 */
@Service
public class ApprovalStaffServiceImpl implements ApprovalStaffService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private StaffMapper staffMapper;

    @Override
    public Result query(ReqDto<QueryApprovalReqDto> req) {

        logger.info("query : req ==>{}", JsonUtils.toString(req));
        QueryApprovalReqDto data = req.getData();

        Staff staff = data.getStaff();

        QueryStaff queryStaff = data.getStaff();

        //分页查询优化
        Integer pageNo = data.getPageNo();
        Integer pageSize = data.getPageSize();
        if (pageNo == null || pageSize == null) {
            pageNo = 1;
            pageSize = 10;
        }
        int offSet = (pageNo - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offSet, pageSize);

        Example example = new Example(Staff.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo(staff);

//        调节查询时间，设置为一天最大和最小时间
        Date entryTimeEnd = queryStaff.getEntryTimeEnd();
        logger.info("entryTimeEnd = " + entryTimeEnd);

        if (entryTimeEnd != null) {
            entryTimeEnd = DateUtil.getEndOfDay(entryTimeEnd);
            criteria.andLessThanOrEqualTo(Staff.PROPERTY_ENTRYTIME, entryTimeEnd);
        }
        Date entryTimeStart = queryStaff.getEntryTimeStart();

        if (entryTimeStart != null) {
            entryTimeStart = DateUtil.getStartOfDay(entryTimeStart);
            criteria.andGreaterThanOrEqualTo(Staff.PROPERTY_ENTRYTIME, entryTimeStart);
        }
//        根据条件分页查询
        List<Staff> staffList = staffMapper.selectByExampleAndRowBounds(example, rowBounds);
        logger.info("query result==>{}", JsonUtils.toString(staff));

        int count = staffMapper.selectCountByExample(example);
        logger.info("count = " + count);

        QueryApprovalResDto resData = new QueryApprovalResDto();
        resData.setPageNo(pageNo);
        resData.setPageSize(pageSize);
        resData.setStaffList(staffList);
        resData.setTotalRow(count);
        Result result = Result.success(resData);

        logger.info("/query响应：{}", JsonUtils.toString(result));

        return result;
    }

    /**
     * 根据id查询
     * @param req
     * @return
     */
    @Override
    public Result detail(ReqDto<QueryDetailReqDto> req) {
        logger.info("/detail请求：==>{}", JsonUtils.toString(req));

        QueryDetailReqDto data = req.getData();
        Integer pageNo = data.getPageNo();
        Integer pageSize = data.getPageSize();
        if (pageNo == null || pageSize == null) {
            pageNo = 1;
            pageSize = 10;
        }
        int offSet = (pageNo - 1) * pageSize;

        List<Staff> staffList = staffMapper.selectBatchPageByIds(data, offSet, pageSize);

        Result result = Result.success(staffList);
        logger.info("/query响应：==>{}", JsonUtils.toString(result));
        return result;

    }

    /**
     * 对员工状态审批更新
     * @param req
     * @return
     */
    @Override
    public Result update(ReqDto<UpdateFlowReqDto> req) {
        logger.info("/update请求：==>{}", JsonUtils.toString(req));

        UpdateFlowReqDto data = req.getData();
        staffMapper.updateBatchPageByIds(data);

        Result result = Result.success("update success");

        logger.info("/update响应：==>{}", JsonUtils.toString(result));
        return result;
    }
}
